package com.ivan.service;

import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ivan.domain.Messages;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("messageService")
@Transactional
public class MessageService {
	
	protected static Logger logger = Logger.getLogger("service");
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	 public List<Messages> getAllMessages() {
		  logger.debug("Retrieving all messages");
		  Session session = sessionFactory.getCurrentSession();
		  Query query = session.createQuery("FROM com.ivan.domain.Messages");
		  return query.list();
	 }
	 
	 public Messages get( Integer id ) {
		  logger.debug("Retrieving message with id of  "+ id);
		  Session session = sessionFactory.getCurrentSession();
		  Messages message = (Messages) session.get(Messages.class, id);
		  return message;
	 }
	 
	 public void add(Messages message) {
		  logger.debug("Adding new person");
		  Session session = sessionFactory.getCurrentSession();
		  session.save(message);
	 }
	 
	 public void delete(Integer id) {
		  logger.debug("Deleting existing message with id of "+ id);
		  Session session = sessionFactory.getCurrentSession();
		  Messages message = (Messages) session.get(Messages.class, id);
		  session.delete(message);
	 }
	 
	 public void edit(Messages message) {
		  logger.debug("Editing existing person");
		  Session session = sessionFactory.getCurrentSession();
		  Messages existingMessage = (Messages) session.get(Messages.class, message.getMessageid());
		  existingMessage.setAccountid(existingMessage.getAccountid());
		  existingMessage.setMessage(message.getMessage());
		  session.save(existingMessage);
	 }

}
